/**
 * Created by szc on 2016/12/26.
 */

var jsonOb = {};
var reconFn = new Function();

function setReconFn(fn){
    //   var test = new Reconnect(this);
            // point.addChild(test);
    reconFn = fn;
}

function socketTouch(json){
    Socket.sendData(json);
}

function socketSend(json, fn) {
    var callFn = fn || new Function('');
//  console.log(json);
    Socket.sendData(json).then(callFn);
}

/**
 * 服务器通讯函数
 * @param action
 * @param data
 * @param fn
 */
function serverDispatch(action, postData, fn) {


    var actionArr = {};
    actionArr.Initial = function () {

        jsonOb.Action = "Initial";
        var parameter = {};
        parameter.PartnerId = jsonOb.PartnerId;
        // parameter.McDebug = 1;
        jsonOb.Parameter = parameter;

        var jsonStr = JSON.stringify(jsonOb);

        return jsonStr;
    };

    actionArr.TouchToken = function () {
        jsonOb.Action = "TouchToken";
        var jsonStr = JSON.stringify(jsonOb);
        return jsonStr;
    };

    actionArr.Play = function (_ob) {
        jsonOb.Action = "Play";
        var parameter = {};
        // parameter.PartnerId="artner01";
        parameter.Type = _ob.type;
        // parameter.DebugMode = _ob.debugMode;
        // parameter.McDebug = 1;

        if (_ob.type == "slot") {
            parameter.Bet = _ob.Bet || "";
            parameter.Denom = _ob.Denom || "";
            parameter.Line = _ob.Line || "";
        } else if (_ob.type == "freeslot") {

        } else {
//          console.log("socket", _ob.type + "是不合法参数");
            return false;
        }
        jsonOb.Parameter = parameter;
        var jsonStr = JSON.stringify(jsonOb);
//      console.log("要发送的json数据包", jsonStr);
        return jsonStr;
    };

    var jsonStr = actionArr[action](postData);

    // console.log(json);

    if(action=="TouchToken"){
        socketTouch(jsonStr);
    } else {
    socketSend(jsonStr, function (data) {
        fn(data);
       });
    }
}

function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}